import { ApiBase } from '@/types/api/apiBase';
import { Monitor } from '@/types/monitor';
import service from '@/utils/http';
import { MonitorInfo } from '../types/monitor';
import isMonitorOnline from '@/utils/monitor/isMonitorOnline';
import formatMinerCount from '@/utils/monitor/formatMinerCount';
import { OrderOptions } from '@/types/OrderOptions';
import currencyListModel from '@/models/currencylist';
import formatHash from '@/utils/format/formatHash';
import currencylist from '@/models/currencylist';
import { AnalysisInfo } from '@/types/AnalysisInfo';

export function fetchMonitor(mid?: number): Promise<{
  allList: Monitor[];
  onlineList: Monitor[];
  versionOptions: OrderOptions[];
}> {
  return new Promise(async (resolve, reject) => {
    const res = await service.get<ApiBase<{ list: Monitor[] }>>(
      `/web/monitor`,
      {
        params: {
          page: 1,
          mid,
        },
      },
    );
    if (res.data.code === 0) {
      const currencyMap = await currencyListModel.getCurrencyMap();

      const versionOptions: OrderOptions[] = [
        {
          text: '版本',
          value: 'all',
        },
      ];
      const list = res.data.result.list.map((monitor) => {
        versionOptions.push({
          text: monitor.version,
          value: monitor.version_num.toString(),
        });
        return {
          ...monitor,
          minerCount: formatMinerCount(monitor.miner, monitor.miner_num),
          groupList: monitor.groupList.map((group) => {
            const currency = currencyMap.get(group.main_coin);
            return {
              ...group,
              miner_count: formatMinerCount(group.miner, group.miner_num),
              hashFormat: formatHash(group.hash, group.main_coin, currency),
            };
          }),
        };
      });
      resolve({
        allList: list,
        onlineList: list.length ? list.filter(isMonitorOnline) : [],
        versionOptions,
      });
    } else {
      reject();
    }
  });
}

export function fetchMonitorInfo(mid: number | string) {
  return new Promise<{ admin_phone: string; info: MonitorInfo }>(
    async (resolve, reject) => {
      const res = await service.get<
        ApiBase<{ admin_phone: string; info: MonitorInfo }>
      >(`/web/monitorinfo?mid=${mid}`);
      if (res.data.code === 0) {
        res.data.result.info.minerCount = formatMinerCount(
          res.data.result.info.miner,
          res.data.result.info.miner_num,
        );
        resolve(res.data.result);
      } else {
        reject();
      }
    },
  );
}

/**
 * v1/web_coin/drop_down?mid=87&date=2021-07-08&day=7
 *
 * v1/web/monitorhash?date=2021-07-08&day=7&mid=87
 * date: 2021-07-08
 day: 7
 mid: 87
 */

export function fetchMonitorHash() {}
